import {
  loginFun
} from '../../utils/api'
import {
  mobileValidate
} from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    pwd: "",
    token: '',
    disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onMobileInput: function (event) {
    this.setData({
      mobile: event.detail.value
    })
  },
  onPwdInput: function (event) {
    this.setData({
      pwd: event.detail.value
    })
  },
  doLogin: function () {
    const {
      mobile,
      pwd,
    } = this.data
    if (mobile && pwd ) {
    // if (mobile && mobileValidate(mobile) && pwd ) {
      this.postlogin();
    } else {
      // title: !mobile ? '请输入手机号' : !mobileValidate(mobile) ? '手机号格式不正确' : !pwd ? '请输入密码' : '',
      wx.showToast({
        title: !mobile ? '请输入手机号' : '',
        icon: "none",
        duration: 3000
      })
    }
  },
  postlogin: function () {
    let that = this
    if (!that.data.disabled) {
      that.setData({
        disabled: true
      })
      loginFun({
        mobile: that.data.mobile,
        password: that.data.pwd
      }).then(res => {
        wx.setStorageSync('userToken', res.user.userToken)
        wx.switchTab({
          url: '/pages/index/index',
        })
        that.setData({
          disabled: false
        })
      }).catch(() => {
        that.setData({
          disabled: false
        })
      })
    }
  },
  getInfo: function () {
    this.$http({
      method: "POST",
      api: 'Apiv1/user/info',
      showLoading: true,
      param: {
        t: new Date().getTime(),
      },
    }, (res, errMsg) => {
      console.log(res);
      uni.setStorageSync('userinfo', res.result);
      if (this.group_id) {
        uni.navigateTo({
          url: '/pages/pin/index?group_id=' + this.group_id
        })
      } else if (this.pin_group_id) {
        uni.navigateTo({
          url: '/pages/pin/pinStock?pin_group_id=' + this.pin_group_id
        })
      } else {
        uni.navigateTo({
          url: '/pages/personalData/personalData1'
        });
      }
    }, (code, errMsg) => {
      uni.showModal({
        title: '系统提示',
        showCancel: false,
        content: errMsg,
      })
    }, (errMsg) => {
      uni.showModal({
        title: '系统提示',
        showCancel: false,
        content: errMsg,
      })
    });
  },
  toPage(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.type == 1 ? '/pages/register/index' : '/pages/forgetPwd/index',
    })
  }
})